<template>
  <el-dialog
    title="自定义列"
    :visible.sync="showColumn"
    v-dialogDrag
    :close-on-click-modal="false"
    width="400px"
    customClass="mes_dialog col_dialog">
    <div class="column_list" :style="{height: maxHeight}">
      <el-scrollbar>
        <el-checkbox-group v-model="checkedList" @change="checkedItemChange">
          <div v-for="item in rowList" :key="item.prop">
            <el-checkbox :label="item">{{item.label}}</el-checkbox>
          </div>
        </el-checkbox-group>
      </el-scrollbar>
    </div>
    <div slot="footer" class="col_footer">
      <el-checkbox
        :indeterminate="isIndeterminate"
        v-model="checkAll"
        @change="checkAllChange">全选</el-checkbox>
      <div class="mes_btn">
        <el-button type="primary" @click="saveCol">保存</el-button>
        <el-button @click="showColumn = false">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'ColumnControl',
  props: {
    colList: {
      type: Array
    }
  },
  data () {
    return {
      showColumn: false, // 是否显示弹框
      checkAll: true, // 是否全选
      isIndeterminate: false, // 是否部分选中
      checkedList: [], // 已选列表
    }
  },
  computed: {
    // 弹框最大高度
    maxHeight() {
      let height = this.rowList.length <= 10 ? this.rowList.length : 10
      return height * 30 + 'px'
    },
    // 所有可选行
    rowList() {
      return this.colList.filter(item => item.noChecked !== true)
    }
  },
  mounted() {
  },
  methods: {
    // 打开方法
    openDialog() {
      // 获取localStorage中存储的列显示配置并设置默认选中列
      let keys = window.localStorage.getItem(this.$route.name)
      if (keys) {
        this.checkedList = this.rowList.filter(item => JSON.parse(keys).some(key => key === item.prop))
      } else {
        this.checkedList = this.rowList
      }
      this.checkAll = this.checkedList.length === this.rowList.length
      this.isIndeterminate = this.checkedList.length > 0 && !this.checkAll
      this.showColumn = true
    },
    // 全选回调
    checkAllChange(val) {
      this.checkedList = val ? this.rowList : []
      this.isIndeterminate = false
    },
    // 单选回调
    checkedItemChange(val) {
      this.checkAll = val.length === this.rowList.length
      this.isIndeterminate = val.length > 0 && !this.checkAll
    },
    // 保存回调
    saveCol() {
      this.showColumn = false
      let checkedKeys = this.checkedList.reduce((pre, cur) => [...pre, cur.prop], [])
      window.localStorage.setItem(this.$route.name, JSON.stringify(checkedKeys))
      this.$emit('saveCol', JSON.stringify(checkedKeys))
    }
  }
}
</script>

<style scoped lang="less">
  .col_footer{
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
  .column_list{
    .el-scrollbar {
      height: 100%;
    }
    .el-checkbox{
      margin: 0px;
      font-size: 14px;
      color: #333;
      height: 30px;
      line-height: 30px;
    }
  }
</style>
<style lang="less">
  .col_dialog{
    .el-dialog__body{
      padding: 10px 20px;
      max-height: 300px;
    }
  }
</style>
